import Vue from "vue";
import store from "@/store";
import VueRouter from "vue-router";
import { Message } from 'element-ui'

import Login from '@/views/login'
import Home from '@/views/home'
import ItemManage from '@/views/home/itemManage'
import PetManage from '@/views/home/petManage'
import UserManage from '@/views/home/userManage'
import RoomManage from '@/views/home/roomManage'
import NotFound from '@/views/404'

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: "/login",
    name: "login",
    component: Login,
  },
  {
    path: '/home',
    name: 'home',
    component: Home,
    redirect: '/home/itemManage',
    children: [
      {
        path: 'itemManage',
        name: 'itemManage',
        component: ItemManage
      },
      {
        path: 'userManage',
        name: 'userManage',
        component: UserManage
      },
      {
        path: 'petManage',
        name: 'petManage',
        component: PetManage
      },
      {
        path: 'roomManage',
        name: 'roomManage',
        component: RoomManage
      },
    ]
  },
  {
    path: '*',
    name: '404',
    component: NotFound
  }
];
console.log(process.env.BASE_URL);
const router = new VueRouter({
  mode: "hash",
  // base: process.env.BASE_URL,
  base: process.env.VUE_APP_ROUTER_BASE_URL || process.env.BASE_URL,
  routes,
});

router.beforeEach(async (to, from, next) => {
  if (to.name === 'login' || to.name === 'register') {
    next()
  } else {
    if (!store.state.login.token) {
      Message.warning('请先登录！')
      next({ name: 'login' })
    } else {
      next()
    }
  }
})

export default router;
